/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-07-01 14:14:00
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-03 02:47:50
 * @FilePath: \keep_motion_api\models\data\dietRecord.model.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// 饮食记录模型
const dietRecordSchema = new Schema({
  user_id: { 
    type: Schema.Types.ObjectId, 
    ref: 'User',
    required: true 
  },
  meal_type: {
    type: String,
    enum: ["breakfast", "lunch", "dinner", "snack"]
  },
  food_items: [{
    name: String,
    calories: Number,
    protein: Number,
    carbs: Number,
    fat: Number
  }],
  total_calories: Number,
  record_date: { type: Date, default: Date.now },
  images: [String],
  related_plan: {
    type: Schema.Types.ObjectId,
    ref: 'TrainingPlan'
  }
});
dietRecordSchema.statics.getRecords = async function (user_id) {
  return await this.find({ user_id });
}
dietRecordSchema.statics.getStats = async function (user_id) {
  return await this.aggregate([
    { $match: { user_id } },
    {
      $group: {
        _id: "$meal_type",
        total_calories: { $sum: "$total_calories" },
        count: { $sum: 1 }
      }
    }
  ]);
}
dietRecordSchema.statics.aiAnalysis = async function (user_id) {
  return await this.aggregate([
    { $match: { user_id } },
    {
      $group: {
        _id: "$meal_type",
        total_calories: { $sum: "$total_calories" },
        count: { $sum: 1 }
      }
    }
  ]);
}
module.exports = mongoose.model('DietRecord', dietRecordSchema);
